package com.auth.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.token.TokenStore;

@EnableAuthorizationServer
@Configuration
 public class AuthServerConfig implements AuthorizationServerConfigurer {
    @Autowired
    private ClientDetailsService clientDetailsService;
    @Autowired
    TokenStore tokenStore;
    @Autowired
    private AuthenticationManager authenticationManager;

    // 令牌端点配置
    @Override
    public void configure(AuthorizationServerSecurityConfigurer authorizationServerSecurityConfigurer) throws Exception {

        authorizationServerSecurityConfigurer.checkTokenAccess("isAuthenticated()").tokenKeyAccess("permitAll()").allowFormAuthenticationForClients();

    }

    // 客户端配置
    @Override
    public void configure(ClientDetailsServiceConfigurer clientDetailsServiceConfigurer) throws Exception {
        ClientDetailsService build = new InMemoryClientDetailsServiceBuilder()
                .withClient("client").autoApprove(false).secret(new BCryptPasswordEncoder().encode("client"))
                .redirectUris("http://127.0.0.1:8001/login/oauth2/code/center-auth", "http://127.0.0.1:8001/login/oauth2/code/center-auth")
                .authorizedGrantTypes("refresh_token", "implicit", "client_credentials", "authorization_code", "password")
                .scopes("all")
                .and()
                .withClient("gateway").secret("gateway").authorizedGrantTypes("refresh_token", "implicit", "client_credentials", "authorization_code", "password").resourceIds("all").and().build();

        clientDetailsServiceConfigurer.withClientDetails(build);
    }

    // 令牌访问端点配置
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer authorizationServerEndpointsConfigurer) throws Exception {
        authorizationServerEndpointsConfigurer.allowedTokenEndpointRequestMethods(HttpMethod.POST)
                .tokenStore(tokenStore).authenticationManager(authenticationManager)
               ;
    }


}
